import numpy as np
import matplotlib.pyplot as plt
# 定义x、y散点坐标
x = [1,2,3,4,5,6,7,8,9,10]
x = np.array(x)
num = [1.0,1.2,1.6,2.6,2.7,3.6,3.7,4.2,3.8,3.7]
y = np.array(num)
x10=[11,12,13,14,15,16,17,18,19,20]
x20=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
y10=[]
# 用3次多项式拟合
f1 = np.polyfit(x, y, 3)
print('f1 is :\n', f1)

p1 = np.poly1d(f1)
print('p1 is :\n', p1)

# 也可使用yvals=np.polyval(f1, x)
yvals = p1(x)  # 拟合y值

print('yvals is :\n', yvals)
y10=p1(x20)
print('预测未来十年产量 :\n', y10)
# 绘图
plot1 = plt.plot(x, y, 's', label='original values')
plot2 = plt.plot(x20, y10, 'r', label='polyfit values')
plt.xlabel('x')
plt.ylabel('y')
plt.legend(loc=4)  # 指定legend的位置右下角
plt.title('polyfitting')
plt.show()